package handler

import (
	"Readme/request"
	"Readme/ted"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Qun(c *gin.Context) {
	var data request.Qun
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "错误",
			"data": nil,
		})
		return
	}
	userId := c.GetUint("userId")
	cont, err := ted.Qun(int(userId), data.Name)
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "ok",
		"data": cont.Id,
	})
}

func Group(c *gin.Context) {
	var data request.Group
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "错误",
			"data": nil,
		})
		return
	}
	userId := c.GetUint("userId")
	cont, err := ted.Group(int(userId), data.UserId)
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "ok",
		"data": cont.Id,
	})
}

func Deltegroup(c *gin.Context) {
	var data request.Group
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "错误",
			"data": nil,
		})
		return
	}
	userId := c.GetUint("userId")
	cont, err := ted.DeltedGroup(int(userId), data.UserId)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "删除失败",
			"data": nil,
		})

	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "删除成功",
		"data": cont.Id,
	})
}

func Updatedgroup(c *gin.Context) {
	var data request.Dated
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "错误",
			"data": nil,
		})
		return
	}
	userId := c.GetUint("userId")
	cont, err := ted.UpdateGroup(int(userId), data.UserId)
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "ok",
		"data": cont.Type,
	})
}
